import { ApiCall } from "tsrpc";
import { ReqSell, ResSell } from "../../../shared/master/auction/PtlSell";
import { player } from "../../../model/fight/body/player";
import { template } from "../../../shared/master/MsgAction";
import manage_auction from "../../model/manage/manage_auction";
import { prop_equip } from "../../../model/bag/prop_equip";
import manage_EquipStrengthen from "../../model/manage/manage_EquipStrengthen";

export default async function (call: ApiCall<ReqSell, ResSell>) {
    call.succ({})
    let user = call.req._player as player;
    let sellItem = user.getBagList()[call.req.idx - 1];
    if (!sellItem) {
        user.sendMsg(template.default, `背包内没有ID为${call.req.idx}的物品噢`)
        return;
    }
    if(!call.req.num){
        call.req.num = 1;
    }
    if(sellItem.num < call.req.num){
        user.sendMsg(template.default, `拍卖失败:你背包内ID[${call.req.idx}]${sellItem.getStyleName()}数量不足${call.req.num}个噢`)
        return;
    }
    sellItem.cont_change(-call.req.num)
    if(sellItem instanceof prop_equip){
        manage_EquipStrengthen.clear(user.uuid,sellItem.data);
    }
    manage_auction.sell(user.name, user.uuid, sellItem, call.req.num)

    user.sendMsg(template.default, `拍卖成功:在您前面还有${manage_auction.auctionList.length}个道具待拍卖哦~`)
}